/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package amd.core.calculation;

import Jama.Matrix;

/**
 *
 * @author Latifah Dwi Nurachma
 */
public class MatrixReduction {

    public static Matrix reduceRow(Matrix matrixToReduce, int nRowLast){
        int newNRow = nRowLast;
        int newNCol = matrixToReduce.getColumnDimension();
        return createNewReducedMatrix(matrixToReduce, newNRow, newNCol);
    }

    public static Matrix reduceColumn(Matrix matrixToReduce, int nColLast){
        int newNRow = matrixToReduce.getRowDimension();
        int newNCol = nColLast;
        return createNewReducedMatrix(matrixToReduce, newNRow, newNCol);
    }

    public static Matrix reduceRowAndColumn(Matrix matrixToReduce, int nRowToReduce, int nColToReduce){
        int newNRow = nRowToReduce;
        int newNCol = nColToReduce;
        return createNewReducedMatrix(matrixToReduce, newNRow, newNCol);
    }

    private static Matrix createNewReducedMatrix(Matrix matrixToReduce, int newNRow, int newNCol){
        Matrix newMatrix = new Matrix(newNRow, newNCol);
        int oldNRow = matrixToReduce.getRowDimension();
        int oldNCol = matrixToReduce.getColumnDimension();
        int i=0, j=0;

        while(i<newNRow){
            while(j<newNCol){
                float newValue = 0;
                if(i<oldNRow && j<oldNCol){
                    newValue = (float) matrixToReduce.get(i, j);
                }
                newMatrix.set(i, j, newValue);
                j++;
            }
            j=0;
            i++;
        }
        return newMatrix;
    }
}

